﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;




namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]

    public class AdminVendorsTests : ATBase
    {
        #region Test Setup and TearDown

        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Add_Vendor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            System.Threading.Thread.Sleep(1500);
            VendorsPage vendorPage = new VendorsPage(homePage);
            vendorPage.AddNewVendorLink.Click();
            vendorPage.CompanyNameField.Value = "Vendor 1";
            vendorPage.FirstNameField.Value = "vFname";
            vendorPage.LastNameField.Value = "vLname";
            vendorPage.EmailField.Value = "vendor@vendorsite.com";
            vendorPage.StreetField.Value = "Vendor Street";
            vendorPage.CityField.Value = "Vendor City";
            vendorPage.CountrySelectList.Select("Canada");
            System.Threading.Thread.Sleep(2000);
            vendorPage.RegionRequiredCheckbox.Checked = false;
            System.Threading.Thread.Sleep(1000);
            vendorPage.PostalCodeField.Value = "X3X 2X2";
            vendorPage.TelephoneField.Value = "555-666-7777";
            vendorPage.CellphoneField.Value = "222-333-4444";
            vendorPage.FaxField.Value = "123-456-7891";
            vendorPage.UpdateLink.ClickNoWait();

            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Assert
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
            TableRow vendorRow = vendorPage.GetVendorRow("Vendor 1");
            Assert.IsTrue(vendorRow.Exists);
        }

        [Test]
        public void Delete_Vendor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            ConfirmDialogHandler dialog = new ConfirmDialogHandler();
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                vendorPage.DeleteLink.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }

            //Assert
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
        }

        [Test]
        public void Edit_Vendor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            System.Threading.Thread.Sleep(1500);
            vendorPage.CompanyNameField.Value = "Test Vendor";
            vendorPage.UpdateLink.ClickNoWait();

            //Assert
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("Test Vendor"));
            TableRow vendorRow = vendorPage.GetVendorRow("Test Vendor");
            Assert.IsTrue(vendorRow.Exists);
        }

        [Test]
        public void Edit_Vendor_Add_Text_Banner()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.BannerAdExpandButton.Click();
            vendorPage.AddBannerLink.ClickNoWait();
            vendorPage.BannerNameField.Value = "Test Banner 1";
            vendorPage.BannerTypeSelectList.Select("Text");
            vendorPage.BannerDescriptionField.Value = "This is a test banner.";
            vendorPage.UpdateLink.Click();

            //Assert
            vendorPage.BannerAdExpandButton.Click();
            Assert.IsTrue(vendorPage.BannerTable.InnerHtml.Contains("Test Banner 1"));
            TableRow bannerRow = vendorPage.GetBannerRow("Test Banner 1");
            Assert.IsTrue(bannerRow.Exists);
            vendorPage.GetBannerEditButton("Test Banner 1").Click();
            Assert.IsTrue(vendorPage.ViewBannerTable.InnerHtml.Contains("This is a test banner."));

        }

        [Test]
        public void Edit_Vendor_Unauthorize_Vendor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.AuthorizeVendorCheckBox.Checked = false;
            vendorPage.UpdateLink.Click();

            //Assert
            vendorPage.UnauthorizedDisplayLink.Click();
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
        }

        [Test]
        public void Edit_Vendor_Authorize_Vendor()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.AuthorizeVendorCheckBox.Checked = false;
            vendorPage.UpdateLink.ClickNoWait();

            //Act
            ieInstance.GoTo(SiteURL + "/Home.aspx");
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.AuthorizeVendorCheckBox.Checked = true;
            vendorPage.UpdateLink.ClickNoWait();

            //Assert
            vendorPage.UnauthorizedDisplayLink.Click();
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
        }

        [Test]
        public void Edit_Vendor_Upload_Logo_Create_Image_Banner()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.UploadLogoLink.ClickNoWait();
            vendorPage.LogoFileUpload.Set(ImagePath);
            vendorPage.UploadSelectedFileLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
            vendorPage.UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);

            vendorPage.GetVendorEditButton("Vendor 1").Click();
            vendorPage.BannerAdExpandButton.Click();
            vendorPage.AddBannerLink.ClickNoWait();
            vendorPage.BannerNameField.Value = "Test Image Banner";
            vendorPage.BannerTypeSelectList.Select("Banner");
            vendorPage.BannerImageSelectList.Select(ImageName);
            vendorPage.UpdateLink.Click();

            //Assert
            vendorPage.BannerAdExpandButton.Click();
            Assert.IsTrue(vendorPage.BannerTable.InnerHtml.Contains("Test Image Banner"));
            TableRow bannerRow = vendorPage.GetBannerRow("Test Image Banner");
            Assert.IsTrue(bannerRow.Exists);
            vendorPage.GetBannerEditButton("Test Image Banner").Click();
            TableCell BannerCell = vendorPage.ViewBannerTable.TableRows[0].TableCells[0];
            Assert.IsTrue(BannerCell.Image(Find.BySrc(s => s.Contains("/" + ImageName))).Exists);
        }

        [Test]
        public void Delete_UnAuthorized_Vendors()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.AddNewVendor("Vendor 2", "vFname2", "vLname2", "vendor2@address.com", "vendor2 street", "vendor2 City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.GetVendorEditButton("Vendor 1").Click();
            System.Threading.Thread.Sleep(1500);
            vendorPage.AuthorizeVendorCheckBox.Checked = false;
            vendorPage.UpdateLink.ClickNoWait();

            //Act 
            ConfirmDialogHandler dialog = new ConfirmDialogHandler();
            using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
            {
                vendorPage.DeleteUnauthorizedVendorsLink.ClickNoWait();
                dialog.WaitUntilExists();
                dialog.OKButton.Click();
            }
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Vendor 1"));
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("Vendor 2"));
        }

        [Test]
        public void Search_Vendors()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Microsoft", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.AddNewVendor("DotNetNuke", "vFname2", "vLname2", "vendor2@address.com", "vendor2 street", "vendor2 City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            vendorPage.AddNewVendor("Apple", "vFname3", "vLname3", "vendor3@address.com", "vendor3 street", "vendor3 City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.VendorSearchField.Value = "DotNetNuke";
            vendorPage.SearchTypeSelectList.Select("Name");
            vendorPage.SearchLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Microsoft"));
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("Apple"));
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("DotNetNuke"));
        }

        [Test]
        [DependsOn("Add_Vendor")]
        public void Add_Duplicate_Vendor_Fail()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");
            VendorsPage vendorPage = new VendorsPage(homePage);

            vendorPage.AddNewVendor("Vendor 1", "vFname", "vLname", "vendor@address.com", "vendor street", "vendor City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(1000);
            ieInstance.GoTo(SiteURL + "/Admin/Vendors.aspx");

            //Act
            vendorPage.AddAdditionalVendor("Vendor 1", "vFname2", "vLname2", "different@EmailAddress.com", "vendor2 street", "vendor2 City", "Canada", "V3V 5F5", "123-456-7891");
            System.Threading.Thread.Sleep(2000);

            //Assert
            Assert.IsTrue(vendorPage.PopUpMessageSpan.InnerHtml.Contains("There was an error adding the Vendor."), "Actual Text: " + vendorPage.ModuleMessageSpan.Text);
            vendorPage.CancelLink.Click();
            Assert.IsFalse(vendorPage.VendorTable.InnerHtml.Contains("different@EmailAddress.com"));
            Assert.IsTrue(vendorPage.VendorTable.InnerHtml.Contains("vendor@address.com"));
        }

        #endregion
    }
}